<?php
defined('BASEPATH') or exit('No direct script access allowed');

class StatisticsTable5 extends AdminController
{

	function __construct()
	{
		parent::__construct();
		$this->sidebar = "Overview-Home";
		$this->load->model('Table5_model');
		$this->load->model('Table5_student_model');
	}

	function Index($dept_id = '', $start_time = '', $end_time = '')
	{
		$this->load->model('Admin_model');
		$result['dept_list'] = $this->Admin_model->query('select distinct dept_id,dept_name from db_admin where dept_id is not null order by dept_id');
		$dept_name = '';
		foreach ($result['dept_list'] as $k => $v) {
			if ($v['dept_id'] == $dept_id) {
				$dept_name = $v['dept_name'];
			}
		}
		if ($dept_name == '鉴湖学院（纺织学院、黄酒学院、健康学院）' || $dept_name == '鉴湖分院') {
			$dept_name = '鉴湖';
		} elseif ($dept_name == '机电工程学院' || $dept_name == '机电工程分院') {
			$dept_name = '机电工程';
		} elseif ($dept_name == '设计与艺术学院（腾讯云国际互联网学院）' || $dept_name == '设计与艺术分院') {
			$dept_name = '设计与艺术';
		} elseif ($dept_name == '交通学院' || $dept_name == '交通分院') {
			$dept_name = '交通';
		} elseif ($dept_name == '公共基础教学部（体育部）、马克思主义学院') {
			$dept_name = '公共基础教学';
		} elseif ($dept_name == '商贸学院' || $dept_name == '商贸分院') {
			$dept_name = '商贸';
		} elseif ($dept_name == '财经学院' || $dept_name == '财经分院') {
			$dept_name = '财经';
		} elseif ($dept_name == '建筑工程学院' || $dept_name == '建筑工程分院') {
			$dept_name = '建筑工程';
		} elseif ($dept_name == '电气电子工程学院' || $dept_name == '电气电子工程分院') {
			$dept_name = '电气电子工程';
		} elseif ($dept_name == '机械工程学院' || $dept_name == '机械工程分院') {
			$dept_name = '机械工程';
		} elseif ($dept_name == '经济管理学院' || $dept_name == '经济管理分院') {
			$dept_name = '经济管理';
		}

		$result['dept_name'] = $dept_name;
		$result['start_time'] = $start_time;
		$result['end_time'] = $end_time;

		$this->load->view('admin/statisticsTable5List', $result);
	}

	function getDatas($params = array(), $isExport = false)
	{
		$role = explode(',', getSess()['sub_role_id']);
		array_push($role, getSess()['role_id']);

		if (in_array(WebRole::Admin[XPHEnum::Value], $role)  || in_array(WebRole::Manage[XPHEnum::Value], $role) || in_array(WebRole::SchoolLeader[XPHEnum::Value], $role)) {
			// $dept_id=false;
		} else {
			$dept_id = getSess()['dept_id'];
		}
		$connar = array();
		if (!$isExport) {
			$params = $_POST;
			$connar = $this->Table5_model->pickPages($connar, $params);
		}
		$connar['items'] = "db_table5.*,db_admin.username";
		$connar['where'] = $this->sqlEscape('db_table5.status=??', array(CommonStatus::UnDeleted[XPHEnum::Value]));
		$connar['join'] = array('db_admin', 'db_admin.id=db_table5.admin_id');
		$connar['orderby'] = 'db_table5.created desc';

		// 权限
		$role = explode(',', getSess()['sub_role_id']);
		array_push($role, getSess()['role_id']);
		if (in_array(WebRole::Manage[XPHEnum::Value], $role)) {
			// $connar['where'].=$this->sqlEscape(' and admin_type=??',array(AdminType::Auth[XPHEnum::Value]));
		} else if (in_array(WebRole::UnitSupervisorGroupLeader[XPHEnum::Value], $role)) {
			$connar['where'] .= $this->sqlEscape(' and dept_name=??', array(getSess()['dept_name']));
		} else if (in_array(WebRole::UnitSupervisor[XPHEnum::Value], $role) || in_array(WebRole::SchoolSupervisor[XPHEnum::Value], $role) || in_array(WebRole::OutsideSupervisor[XPHEnum::Value], $role)) {
			$connar['where'] .= $this->sqlEscape(' and admin_id=??', array(getSess()['id']));
		}

		//开始时间,结束时间
		if (isset($params['start_time']) && isset($params['end_time']) && $params['start_time'] != '' && $params['end_time'] != '') {
			$connar['where'] .= $this->sqlEscape(' and db_table5.sub_time>?? and db_table5.sub_time<??', array(strtotime($params['start_time']), strtotime($params['end_time'])));
		}

		if (isset($params['admin_type']) && $params['admin_type'] > -1) {
			$connar['where'] .= $this->sqlEscape(" and admin_type=?? ", array($params['admin_type']));
		}
		if (isset($params['param'])) {
			if ($params['param'] == '机电工程' || $params['param'] == '电气电子工程' || $params['param'] == '机械工程') {
				$connar['where'] .= $this->sqlLikeEscape(" and ((db_table5.teacher_dept_name like '%??%' or db_table5.unit_name like '%??%') or (db_table5.teacher_dept_name like '%??%' or db_table5.unit_name like '%??%') or (db_table5.teacher_dept_name like '%??%' or db_table5.unit_name like '%??%'))", array('机电', '机电', '电气电子工程', '电气电子工程', '机械工程', '机械工程'));
			} else if ($params['param'] == '建筑工程') {
				$connar['where'] .= $this->sqlLikeEscape(" and (db_table5.teacher_dept_name like '%??%' or db_table5.unit_name like '%??%')", array('建', '建'));
			} else {
				$connar['where'] .= $this->sqlLikeEscape(" and (db_table5.teacher_dept_name like '%??%' or db_table5.unit_name like '%??%')", array($params['param'], $params['param']));
			}
		}


		if (!$isExport) {
			$data = $this->Table5_model->get_page_list($connar);
			// echo $this->db->last_query();
		} else {
			$data["ret"] = $this->Table5_model->get_list_full($connar);
		}
		foreach ($data['ret'] as $k => $v) {
			$data['ret'][$k]['created'] = date('Y-m-d H:i:s', $v['created']);
			$data['ret'][$k]['sub_time'] = date('Y-m-d', $v['sub_time']);
		}

		if (!$isExport) {
			echo json_encode($data);
		} else {
			return $data["ret"];
		}
	}


	function Export($id, $isExport = false)
	{

		$params = array('id' => $id);
		$connar['items'] = "db_admin.username,db_table5.*";
		$connar['join'][0] = array('db_admin', 'db_admin.id=db_table5.admin_id');
		$connar['where'] = $this->sqlEscape('db_table5.status=?? and db_table5.id=??', array(CommonStatus::UnDeleted[XPHEnum::Value], $id));
		$result = $this->Table5_model->get_single_full($connar);
		$result['sub_time'] = date('Y-m-d', $result['sub_time']);
		$result['mentor_time'] = date('Y-m-d', $result['mentor_time']);

		$result['data'] = $this->Table5_student_model->get_list(array(), 'id asc', array('table5_id' => $id));
		$num = 0;
		$student = '';

		foreach ($result['data'] as $k => $v) {
			$num = $k + 1;
			$student .= $num . '.' . $v['student_class_name'] . '-' . $v['student_name'] . '-' . $v['contact_phone'] . '<br>';
		}
		$student .= '共有' . $num . '人在本单位实习';
		$data = "<div class='content'>			
			<h2>学生顶岗实习巡查记录表</h2>
			<div>
				<h4>一、基本数据</h4>
				<p class='item'>
					1.巡查部门：" . $result['teacher_dept_name'] . "<br>
					2.巡查人员:" . $result['username'] . "<br>
					3.巡查时间:" . $result['sub_time'] . "<br>
					4.单位名称:" . $result['unit_name'] . "<br>
					5.单位地址:" . $result['unit_address'] . "<br>
					6.单位联系人:" . $result['unit_contact'] . "<br>
					7.单位电话:" . $result['unit_phone'] . "<br>
					8.实习巡查任务:" . $result['task'] . "
				</p>
				<h4>二、巡查记录</h4>
				<p >
					<h5>【学生信息】</h5>
					<div class='item'>" . $student . "
					</div>
				</p>
				<p>
					<h5>【巡查调研】</h5>
					<div class='item'>
						1.【职业道德】" . (new Table5Eval)->show($result['eval1']) . '' . $result['eval1_remark'] . "<br>
						2.【工作态度】" . (new Table5Eval)->show($result['eval2']) . '' . $result['eval2_remark'] . "<br>
						3.【专业水平】" . (new Table5Eval)->show($result['eval3']) . '' . $result['eval3_remark'] . "<br>
						4.【团队合作】" . (new Table5Eval)->show($result['eval4']) . '' . $result['eval4_remark'] . "<br>
						5.【沟通能力】" . (new Table5Eval)->show($result['eval5']) . '' . $result['eval5_remark'] . "<br>
						6.【创新能力】" . (new Table5Eval)->show($result['eval6']) . '' . $result['eval6_remark'] . "<br>
						7.【心理素质】" . (new Table5Eval)->show($result['eval7']) . '' . $result['eval7_remark'] . "<br>
					</div>
					<div>
						<h5>【实习单位意见及建议】</h5>
						<p style='text-indent:25px'>" . $result['advice'] . "
						</p>
					</div>
					<p class='item' style='margin-top:5%'>
						单位负责人或企业导师签名:" . $result['mentor_name'] . "<br>
						联系电话:" . $result['mentor_phone'] . "<br>
						时间：" . $result['mentor_time'] . "
					</p>
				</p>
				<p>
					<h4>三、巡查情况小结</h4>
					<div class='item'>" . $result['summary'] . "
					</div>
				</p>
			</div>
		</div>";
		if (!$isExport) {
			echo $data;
			$this->load->view("admin/statisticstable5Export", $params);
		} else {
			$this->load->library('PHPWord/PHPWord');
			$PHPWord = new PHPWord();
			$section = $PHPWord->createSection();
			$PHPWord->addFontStyle('rStyle', array('bold' => true, 'color' => '000000', 'size' => 16));
			$PHPWord->addParagraphStyle('pStyle', array('align' => 'center'));
			$section->addText("浙江工业职业技术学院学生顶岗实习巡查记录表", 'rStyle', 'pStyle');
			$section->addTextBreak(2);
			$PHPWord->addFontStyle('fStyle', array('bold' => false, 'color' => '000000', 'size' => 10));
			$PHPWord->addFontStyle('xtitle', array('bold' => false, 'color' => '000000', 'size' => 12));
			$PHPWord->addFontStyle('li_title', array('bold' => false, 'color' => '000000', 'size' => 11));
			$PHPWord->addParagraphStyle('pfirstStyle', array('indentation' => array('firstLine' => 460), 'lineHeight' => 1.0, 'spacing' => 120));
			$num = 0;
			$section->addText("一、基本数据", 'xtitle', 'pfirstStyle');
			$section->addText("  1.巡查部门：" . $result['teacher_dept_name'], 'fStyle');
			$section->addText("  2.巡查人员:" . $result['username'], 'fStyle');
			$section->addText("  3.巡查时间:" . $result['sub_time'], 'fStyle');
			$section->addText("  4.单位名称:" . $result['unit_name'], 'fStyle');
			$section->addText("  5.单位地址:" . $result['unit_address'], 'fStyle');
			$section->addText("  6.单位联系人:" . $result['unit_contact'], 'fStyle');
			$section->addText("  7.单位电话:" . $result['unit_phone'], 'fStyle');
			$section->addText("  8.实习巡查任务:" . $result['task'], 'fStyle');
			$section->addTextBreak(1);
			$section->addText("二、巡查记录", 'xtitle', 'pfirstStyle');
			$section->addText("  【学生信息】", 'li_title');
			$num = 0;
			foreach ($result['data'] as $k => $v) {
				$num++;
				$section->addText($num . '.' . $v['student_class_name'] . '-' . $v['student_name'] . '-' . $v['contact_phone'], 'fStyle');
			}
			$section->addText("    共有" . $num . "人在本单位实习", 'fStyle');
			$section->addTextBreak(1);
			$section->addText("  【巡查调研】", 'li_title');
			$section->addText("     1.【职业道德】" . (new Table5Eval)->show($result['eval1']) . "" . $result['eval1_remark'], 'fStyle');
			$section->addText("     2.【工作态度】" . (new Table5Eval)->show($result['eval2']) . "" . $result['eval2_remark'], 'fStyle');
			$section->addText("     3.【专业水平】" . (new Table5Eval)->show($result['eval3']) . "" . $result['eval3_remark'], 'fStyle');
			$section->addText("     4.【团队合作】" . (new Table5Eval)->show($result['eval4']) . "" . $result['eval4_remark'], 'fStyle');
			$section->addText("     5.【沟通能力】" . (new Table5Eval)->show($result['eval5']) . "" . $result['eval5_remark'], 'fStyle');
			$section->addText("     6.【创新能力】" . (new Table5Eval)->show($result['eval6']) . "" . $result['eval6_remark'], 'fStyle');
			$section->addText("     7.【心理素质】" . (new Table5Eval)->show($result['eval7']) . "" . $result['eval7_remark'], 'fStyle');
			$section->addTextBreak(1);
			$section->addText("  【实习单位意见及建议】", 'li_title');
			$section->addTextBreak(1);
			$section->addText("      " . $result['advice'], 'fStyle');
			$section->addText("   单位负责人或企业导师签名:" . $result['mentor_name'], 'fStyle');
			$section->addText("   联系电话:" . $result['mentor_phone'], 'fStyle');
			$section->addText("   时间：" . $result['mentor_time'], 'fStyle');
			$section->addTextBreak(1);
			$section->addText("三、巡查情况小结", 'xtitle', 'pfirstStyle');
			$section->addText($result['summary'], 'fStyle');


			$write = new PHPWord_Writer_Word2007($PHPWord);
			$director = "upload/export/" . date('Ymd', time()) . "/";
			if (!file_exists($director)) {
				//检查是否有该文件夹，如果没有就创建，并给予最高权限
				mkdir($director, 0777);
			}
			$file_name = '浙江工业职业技术学院学生顶岗实习巡查记录表' . date('Ymd') . '.docx';
			$write->save($director . iconv('utf-8', 'gb2312', $file_name));
			exit(retJson('返回地址', true, $director . $file_name));
		}
	}
}
